package com.fling.hero.state;

import com.badlogic.gdx.math.Vector2;
import com.fling.Locator;
import com.fling.entity.CollisionEntity;
import com.fling.objects.Hero;
import com.fling.observer.Event;

public class ThirdJumpState extends HeroState {

	private INPUT jump;
	private boolean holdClimb;
	private Vector2 vel;

	public ThirdJumpState(Hero h, INPUT jump) {
		super(h);
		this.jump = jump;
		type = STATE_TYPE.THIRD_JUMP_STATE;
		determineJump();
	}

	public void determineJump() {
		switch (jump) {
		case JUMP_LEFT:
			vel.set(-hero.X_JUMP_VEL, 0);
			hero.setRightSide(false);
			break;
		case JUMP_DOWN:
			break;
		case JUMP_RIGHT:
			vel.set(hero.X_JUMP_VEL, 0);
			hero.setRightSide(true);
			break;
		case JUMP_UP:
			break;
		default:
			break;
		}
		hero.setSprite(hero.getFlySprite());
		hero.setFlipX(!hero.isRightSide());
	}

	@Override
	public void update(float delta) {
		tmp.set(vel);
		hero.getBody().setLinearVelocity(
				tmp.scl(Locator.getXGameSpeed() * Locator.getOverallGameSpeed() * delta, Locator.getYGameSpeed() * Locator.getOverallGameSpeed()
						* delta));
	}

	@Override
	public void newAction(INPUT input) {
		switch (input) {
		case CLIMB:
			holdClimb = true;
			break;
		case FALL:
			holdClimb = false;
			break;
		default:
			// hero is jumping
			break;
		}
	}

	@Override
	public void enter() {
		holdClimb = false;
		vel = new Vector2();
		hero.getSubject().notify(hero, Event.EVENT_THIRD_JUMP);
	}

	@Override
	public void exit() {
		if (holdClimb) {
			hero.changeState(new ClimbingState(hero));
		} else {
			hero.changeState(new FallingState(hero));
		}
	}

	@Override
	public void collide(CollisionEntity otherBody) {
		if (otherBody.getType().equals(TYPE.WALL) && hero.isExists()) {
			hero.getSubject().notify(hero, Event.EVENT_HIT_WALL);
			exit();
		} else if (otherBody.getType().equals(TYPE.BLOCK)) {
			hero.die();
			vel.set(0, 0);
			hero.getBody().setLinearVelocity(vel);
		}

	}
}
